<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字段名称" prop="fieldName">
        <el-input
          v-model="queryParams.fieldName"
          placeholder="请输入字段名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段别名" prop="fieldAlias">
        <el-input
          v-model="queryParams.fieldAlias"
          placeholder="请输入字段别名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段类型" prop="fieldType">
        <el-select v-model="queryParams.fieldType" placeholder="请选择字段类型" clearable size="small">
          <el-option
            v-for="dict in fieldTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:fields:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:fields:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:fields:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:fields:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fieldsList" @selection-change="handleSelectionChange" :stripe="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" width="150" align="center" prop="index" />
<!--      <el-table-column label="表编号" align="center" prop="tableNo" />-->
      <el-table-column label="字段名称" align="left" prop="fieldName" show-overflow-tooltip/>
      <el-table-column label="字段别名" align="left" prop="fieldAlias" show-overflow-tooltip/>
      <el-table-column label="字段类型" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          {{scope.row.fieldType | formatFieldType}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:fields:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:fields:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改档案字段设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
           <el-form-item label="表编号" prop="tableNo">
             <el-input v-model="form.tableNo" placeholder="请输入表编号" />
           </el-form-item>
         </el-col>
          <el-col :span="12">
            <el-form-item label="字段名称" prop="fieldName">
              <el-input v-model="form.fieldName" placeholder="请输入字段名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="字段别名" prop="fieldAlias">
              <el-input v-model="form.fieldAlias" placeholder="请输入字段别名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字段长度" prop="fieldLength">
              <el-input v-model="form.fieldLength" placeholder="请输入字段长度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="顺序号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入顺序号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="默认值" prop="fieldDefaultValue">
              <el-input v-model="form.fieldDefaultValue" placeholder="请输入默认值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="字段类型" prop="fieldType">
              <el-select v-model="form.fieldType" placeholder="请选择字段类型">
                <el-option
                  v-for="dict in fieldTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字典类型" prop="fieldDicType">
              <el-select v-model="form.fieldDicType" placeholder="请选择字典类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否主键">
              <el-radio-group v-model="form.isPk">
                <el-radio
                  v-for="dict in isPkOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否可以为空">
              <el-radio-group v-model="form.isNull">
                <el-radio
                  v-for="dict in isNullOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否标题">
              <el-radio-group v-model="form.isTitle">
                <el-radio
                  v-for="dict in isTitleOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否列表展示">
              <el-radio-group v-model="form.isList">
                <el-radio
                  v-for="dict in isListOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否可被查询">
              <el-radio-group v-model="form.isQuery">
                <el-radio
                  v-for="dict in isQueryOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否可被编辑">
              <el-radio-group v-model="form.isEdit">
                <el-radio
                  v-for="dict in isEditOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否加密">
              <el-radio-group v-model="form.isEncrypt">
                <el-radio
                  v-for="dict in isEncryptOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="删除标志">
              <el-radio-group v-model="form.delFlag">
                <el-radio
                  v-for="dict in delFlagOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">

            <el-form-item label="输入方式">
              <el-radio-group v-model="form.fieldInputType">
                <el-radio label="0">普通</el-radio>
                <el-radio label="1">字典选择</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFields, getFields, delFields, addFields, updateFields, exportFields } from "@/api/system/fields";

export default {
  name: "Fields",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      fNames:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 档案字段设置表格数据
      fieldsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否主键字典
      isPkOptions: [],
      // 是否可以为空字典
      isNullOptions: [],
      // 是否标题字典
      isTitleOptions: [],
      // 是否列表展示字典
      isListOptions: [],
      // 是否可被查询字典
      isQueryOptions: [],
      // 是否可被编辑字典
      isEditOptions: [],
      // 是否加密字典
      isEncryptOptions: [],
      // 删除标志字典
      delFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fieldName: null,
        fieldAlias: null,
        fieldType: null,
      },
      // 表单参数
      form: {},
      // 字段类型选择
      fieldTypeOptions:[
        {
          dictValue:'int',
          dictLabel:'整型'
        },
        {
          dictValue:'varchar',
          dictLabel:'字符'
        },
        {
          dictValue:'text',
          dictLabel:'长文本'
        },
        {
          dictValue:'decimal',
          dictLabel:'浮点数'
        },
        {
          dictValue:'datetime',
          dictLabel:'日期'
        }
      ],
      // 表单校验
      rules: {
        tableNo:[
          { required: true, message: "表编号不能为空", trigger: "blur" }
        ],
        fieldName: [
          { required: true, message: "字段名称不能为空", trigger: "blur" }
        ],
        fieldAlias: [
          { required: true, message: "字段别名不能为空", trigger: "blur" }
        ],
        fieldType: [
          { required: true, message: "字段类型不能为空", trigger: "change" }
        ],
        fieldInputType: [
          { required: true, message: "输入方式不能为空", trigger: "blur" }
        ],
        fieldLength: [
          { required: true, message: "字段长度不能为空", trigger: "blur" }
        ],
        fieldDefaultValue: [
          { required: true, message: "默认值不能为空", trigger: "blur" }
        ],
        isPk: [
          { required: true, message: "是否主键不能为空", trigger: "blur" }
        ],
        isNull: [
          { required: true, message: "是否可以为空不能为空", trigger: "blur" }
        ],
        isTitle: [
          { required: true, message: "是否标题不能为空", trigger: "blur" }
        ],
        isList: [
          { required: true, message: "是否列表展示不能为空", trigger: "blur" }
        ],
        isQuery: [
          { required: true, message: "是否可被查询不能为空", trigger: "blur" }
        ],
        isEdit: [
          { required: true, message: "是否可被编辑不能为空", trigger: "blur" }
        ],
        isEncrypt: [
          { required: true, message: "是否加密不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "顺序号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.isPkOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isNullOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isTitleOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isListOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isQueryOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isEditOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isEncryptOptions = response.data;
    });
    this.getDicts("sys_common_status").then(response => {
      this.delFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询档案字段设置列表 */
    getList() {
      this.loading = true;
      listFields(this.queryParams).then(response => {
        this.fieldsList = response.rows;
        this.fieldsList.forEach((item,i) => {
          item.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + i + 1
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否主键字典翻译
    isPkFormat(row, column) {
      return this.selectDictLabel(this.isPkOptions, row.isPk);
    },
    // 是否可以为空字典翻译
    isNullFormat(row, column) {
      return this.selectDictLabel(this.isNullOptions, row.isNull);
    },
    // 是否标题字典翻译
    isTitleFormat(row, column) {
      return this.selectDictLabel(this.isTitleOptions, row.isTitle);
    },
    // 是否列表展示字典翻译
    isListFormat(row, column) {
      return this.selectDictLabel(this.isListOptions, row.isList);
    },
    // 是否可被查询字典翻译
    isQueryFormat(row, column) {
      return this.selectDictLabel(this.isQueryOptions, row.isQuery);
    },
    // 是否可被编辑字典翻译
    isEditFormat(row, column) {
      return this.selectDictLabel(this.isEditOptions, row.isEdit);
    },
    // 是否加密字典翻译
    isEncryptFormat(row, column) {
      return this.selectDictLabel(this.isEncryptOptions, row.isEncrypt);
    },
    // 删除标志字典翻译
    delFlagFormat(row, column) {
      return this.selectDictLabel(this.delFlagOptions, row.delFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recNo: null,
        tableNo: null,
        fieldName: null,
        fieldAlias: null,
        fieldType: null,
        fieldInputType: "0",
        fieldDicType: null,
        fieldLength: null,
        fieldDefaultValue: null,
        isPk: "Y",
        isNull: "Y",
        isTitle: "Y",
        isList: "Y",
        isQuery: "Y",
        isEdit: "Y",
        isEncrypt: "Y",
        delFlag: "0",
        orderNo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recNo)
      this.fNames = selection.map(item => item.fieldName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加档案字段设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recNo = row.recNo || this.ids
      getFields(recNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改档案字段设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recNo != null) {
            updateFields(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFields(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recNos = row.recNo || this.ids;
      const fieldNames = row.fieldName || this.fNames
      this.$confirm('是否确认删除档案字段设置名称为"' + fieldNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFields(recNos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有档案字段设置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFields(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  },
  filters:{
    formatFieldType(val){
      switch(val){
        case "int":
          return "整型"
          break
        case "varchar":
          return "字符"
        break
        case "text":
          return "长文本"
          break
        case "decimal":
          return "浮点数"
          break
        case "datetime":
          return "日期"
          break
      }
    }
  }
};
</script>
